<?php
/**
 * 拉卡拉发发送对账需求
 *
 **/
#error_reporting ( 0 );
date_default_timezone_set("Asia/Shanghai");
set_time_limit(0);
if (isset($_SERVER['REQUEST_URI'])) {
    die('error');
    
}
if (!defined('API_ROOT_PATH')) {
    define('API_ROOT_PATH', dirname(__FILE__));
}

//引入配置文件
require_once API_ROOT_PATH . '/common/common.php';

function getNextSleepSecond() {
    $date1 = time();
    $date2 = date('Y-m-d 05:00:00', strtotime('+1 day', $date1));
    //$date2 = strtotime('+ 1 minutes', $date1);//测试专用
    $date2 = strtotime($date2);
    $s = $date2 - $date1;

    $date1 = date('Y-m-d H:i:s', $date1);
    $date2 = date('Y-m-d H:i:00', $date2);

    logUtil("currtime: $date1 ，nexttime:$date2 ，timediff:$s");

    return $s;
}

while (true) {

    $sleeptimes = getNextSleepSecond();
    logUtil("sleep:" . $sleeptimes, 'INFO');
    sleep($sleeptimes);

    $ver = $config['ver']; //协议版本号
    $merId = $config['merId']; //商户号
    $reqType = 'B0008'; //请求业务类型
    $today = date('Ymd');
    $startDate = $today; //对账开始日期
    $endDate = $today; //对账结束日期
    $notifyAddr = $config['notifyAddrUrl']; //通知下载地址
    $ext1 = ''; //扩展字段1
    $ext2 = ''; //扩展字段2
    //公钥
    $pingtaiPublicKey = $config['pk'];
    //私钥
    $merPrivateKey = $config['rk'];

    //商户随机3DES对称密钥
    $merDesStr = md5(time());
    //时间戳
    $dateStr = date('YmdHis') . mt_rand(100000, 999999);
    //时间戳拼接对称密钥
    $encKeyStr = $dateStr . $merDesStr;
    //用公钥加密
    $encrypted = "";
    openssl_public_encrypt($encKeyStr, $encrypted, $pingtaiPublicKey);
    //密钥密文
    $encKey = strtoupper(bin2hex($encrypted));
    //业务数据
    $map = array(
        'startDate' => $startDate,
        'endDate' => $endDate,
        'notifyAddr' => $notifyAddr,
        'ext1' => $ext1,
        'ext2' => $ext2,
    );
    $json = json_encode_s($map);
    //使用对称密钥加密以后的json1
    $json1 = des_encrypt($merDesStr, $json);
    $macStr = $merId . $ver . $dateStr . $reqType . $json1;
    //商户私钥加密以后的mac
    $macStr = sha1($macStr);
    openssl_private_encrypt($macStr, $maced, $config['rk']); //私钥加密
    $mac = strtoupper(bin2hex($maced));

    //需要请求的数据
    $http_data = array(
        'ver' => $ver,
        'merId' => $merId,
        'ts' => $dateStr,
        'reqType' => $reqType,
        'encKey' => $encKey,
        'encData' => $json1,
        'mac' => $mac,
    );
    /* * ********************发送对账请求************** */
    $request = HttpClient::quickPost($config['ppayGateUrl'], $http_data);
    $retMap = json_decode($request, true);
    if ($retMap['retCode'] != '0000') {
        logUtil($retMap['retCode'] . ":" . $retMap['retMsg'], 'ERR');
    } else {
        $retMacStr = $retMap["retCode"] . $retMap["retMsg"] . $retMap["merId"] . $retVer = $retMap["ver"] . $retMap["ts"] . $retMap["reqType"] . $retMap["encData"];
        $retMacStr = sha1(mb_convert_encoding($retMacStr, 'gbk', 'utf-8'));
        //SHA-1加密响应返回的mac
        $reqMacStr1 = '';
        openssl_public_decrypt(hex2bin(strtolower($retMap["mac"])), $reqMacStr1, $pingtaiPublicKey);
        if ($retMacStr != $reqMacStr1) {
            logUtil('MAC校验失败', 'ERR');
        } else {
            $reqData = des_decrypt($merDesStr, $retMap["encData"]);
            if (empty($reqData)) {
                logUtil('解密业务参数失败', 'ERR');
            } else {
                $reqData = json_decode($reqData, true);
                logUtil('Send request success : startDate ' . $reqData['startDate'] . ' - endData:' . $reqData['endDate']);
            }
            unset($reqData);
        }
    }
    unset($retMap);
}
